﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using fanfu.Business;

public partial class BrowseLimit : BasePage
{
    //所有浏览限制信息
    private DataTable __BrowseLimit = new DataTable();

    #region 自定义
    /// <summary>
    /// 绑定全部数据到Repeater
    /// </summary>
    /// <param name="rptDataList">要绑定的Repeater对象</param>
    /// <param name="page">设置当前页,从1开始</param>
    private void BindAllData(Repeater rptDataList, int page)
    {
        //给数据列表赋值
        DataTable __BrowseLimit = new BrowseLimitRule().GetAllBrowseLimitInfo();
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = __BrowseLimit.DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = __AdminPageSize;
        pds.CurrentPageIndex = page - 1;
        rptDataList.DataSource = pds;
        rptDataList.DataBind();

        //创建的分页HTML并赋值给前台
        this.paging.InnerHtml = CreateAdminPaging(__BrowseLimit.Rows.Count, page);
    }
    #endregion

    #region 页面加载
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //绑定列表并转到第一页
            BindAllData(this.rptDataList, 1);
        }
        else
        {
            string controlName = Request.Params.Get("__EVENTTARGET");
            string eventArgument = Request.Params.Get("__EVENTARGUMENT");
            //请求是否来自分页
            if (controlName == "pagecut" && eventArgument != "")
            {
                int current = Convert.ToInt32(eventArgument);
                //绑定列表并转到请求页
                BindAllData(this.rptDataList, current);
            }
        }
    }
    #endregion

    #region 添加事件
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        int result = new BrowseLimitRule().InsertBrowseLimit(this.txtAreaorIP.Value.Trim());
        if (result == 1)
        {
            RunJavascript("PopupMsg('success','恭喜您,添加成功！');", true);
            this.txtAreaorIP.Value = string.Empty;
        }
        else
        {
            RunJavascript("PopupMsg('error','错误,添加失败！');", true);
        }

        //绑定列表并转到第一页
        BindAllData(this.rptDataList, 1);
    }
    #endregion

    #region 删除事件
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int result = 0;
        string deleteItems = this.deleteID.Value;
        if (!string.IsNullOrEmpty(deleteItems))
        {
            string[] values = deleteItems.Split(',');
            foreach (string item in values)
            {
                result += new BrowseLimitRule().DeleteBrowseLimit(item);
            }
            if (result == values.Length)
                RunJavascript("PopupMsg('success','恭喜您,删除成功！');", true);
            else
                RunJavascript("PopupMsg('error','错误,删除失败！');", true);
        }

        //绑定列表并转到第一页
        BindAllData(this.rptDataList, 1);
    }
    #endregion
}